﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace GeoPlugin
{
    /// <summary>
    /// Interaction logic for EarthViewBox.xaml
    /// </summary>
    public partial class EarthViewBox : UserControl
    {

        public Trackball trackball;

        public EarthViewBox()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(EarthViewBox_Loaded);
            EarthViewport3D.Loaded += new RoutedEventHandler(Viewport3D_Loaded);
            EarthViewport3D.MouseRightButtonDown += new MouseButtonEventHandler(Viewport3D_MouseRightButtonDown);
            rec.MouseRightButtonDown += new MouseButtonEventHandler(rec_MouseRightButtonDown);
        }

        private void EarthViewBox_Loaded(object sender, RoutedEventArgs e)
        {
            this.trackball = new Trackball();
            this.trackball.RotateEventSource = EarthViewport3D;
        }

        public void setTrackBallSource(FrameworkElement zoomSource)
        {
            this.trackball.ZoomEventSource = zoomSource;
            EarthViewport3D.Camera.Transform = trackball.Transform;
        }

        void rec_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            Viewport3D_MouseRightButtonDown(sender, null);
        }

        void Viewport3D_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            rec.Width = rec.Height = 0;

        }

        void Viewport3D_Loaded(object sender, RoutedEventArgs e)
        {
            //   StoryboardInit();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        public void OnMouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs args)
        {
            t.Content = "Not Mapped";
            System.Windows.Point mouseposition = args.GetPosition(this);

            rec.Height = 2;
            rec.Width = 2;
            Canvas.SetTop(rec, mouseposition.Y);
            Canvas.SetLeft(rec, mouseposition.X);

            mouseposition = args.GetPosition(EarthViewport3D);
            Point3D testpoint3D = new Point3D(mouseposition.X, mouseposition.Y, 0);
            PointHitTestParameters pointparams = new PointHitTestParameters(mouseposition);

            //test for a result in the Viewport3D
            VisualTreeHelper.HitTest(EarthViewport3D, null, HTResult, pointparams);

            LoadMap();
        }

        private Bitmap LoadImage(String path)
        {
            Stream s = this.GetType().Assembly.GetManifestResourceStream(path);
            return new Bitmap(s);
        }

        private void LoadMap()
        {

            System.Windows.Controls.Image finalImage = new System.Windows.Controls.Image();
            finalImage.Width = 200;
            BitmapImage logo = new BitmapImage();
            logo.BeginInit();
            MemoryStream ms = new MemoryStream();
            String mapName;

            MapControl mapControl = null;

            switch (t.Content.ToString())
            {
                case "Europe":
                    mapName = "Europe";
                    EuropeViewBox europe = new EuropeViewBox();
                    mapControl = new MapControl(europe, mapName);
                    break;
                case "Africa":
                    mapName = "Africa";
                    AfricaViewBox africa = new AfricaViewBox();
                    mapControl = new MapControl(africa, mapName);
                    break;
                case "North America":
                    mapName = "North America";
                    NorthAmericaViewBox northAmerica = new NorthAmericaViewBox();
                    mapControl = new MapControl(northAmerica, mapName);
                    break;
                case "Australia":
                    mapName = "Australia";
                    AustraliaViewBox australia = new AustraliaViewBox();
                    mapControl = new MapControl(australia, mapName);
                    break;
                case "Asia":
                    mapName = "Asia";
                    AsiaViewBox asia = new AsiaViewBox();
                    mapControl = new MapControl(asia, mapName);
                    break;
                case "South America":
                    mapName = "South America";
                    SouthAmericaViewBox southAmerica = new SouthAmericaViewBox();
                    mapControl = new MapControl(southAmerica, mapName);
                    break;
                case "Antartica":
                    mapName = "Antartica";
                    AntarcticaViewBox antarctica = new AntarcticaViewBox();
                    mapControl = new MapControl(antarctica, mapName);
                    break;
                default:

                    mapName = "NoCountry";
                    break;
            }

            if (!mapName.Equals("NoCountry"))
            {
                this.Opacity = 0;

                GeoPlugin.AddBackButton();
                ((ObjectControl)((Grid)this.Parent).Parent).MainGrid.Children.Add(mapControl);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rawresult"></param>
        /// <returns></returns>
        public HitTestResultBehavior HTResult(System.Windows.Media.HitTestResult rawresult)
        {
            RayHitTestResult rayResult = rawresult as RayHitTestResult;

            if (rayResult != null)
            {
                RayMeshGeometry3DHitTestResult rayMeshResult = rayResult as RayMeshGeometry3DHitTestResult;
                if (rayMeshResult != null)
                {
                    GeometryModel3D hitgeo = rayMeshResult.ModelHit as GeometryModel3D;
                    MeshGeometry3D geom = rayMeshResult.MeshHit;

                    // pull the barycentric coordinates of the intersection point
                    double vertexWeight1 = rayMeshResult.VertexWeight1;
                    double vertexWeight2 = rayMeshResult.VertexWeight2;
                    double vertexWeight3 = rayMeshResult.VertexWeight3;

                    // the indices in to where the actual intersection occurred
                    int index1 = rayMeshResult.VertexIndex1;
                    int index2 = rayMeshResult.VertexIndex2;
                    int index3 = rayMeshResult.VertexIndex3;

                    // texture coordinates of the three vertices hit
                    System.Windows.Point texCoord1 = geom.TextureCoordinates[index1];
                    System.Windows.Point texCoord2 = geom.TextureCoordinates[index2];
                    System.Windows.Point texCoord3 = geom.TextureCoordinates[index3];

                    // get the final uv values based on the barycentric coordinates
                    System.Windows.Point finalPoint = new System.Windows.Point(texCoord1.X * vertexWeight1 +
                                                 texCoord2.X * vertexWeight2 +
                                                 texCoord3.X * vertexWeight3,
                                                 texCoord1.Y * vertexWeight1 +
                                                 texCoord2.Y * vertexWeight2 +
                                                 texCoord3.Y * vertexWeight3);

                    double x = finalPoint.X * 512;
                    double y = finalPoint.Y * 256;



                    if (((x > 236) && (x < 316) && (y > 82) && (y < 111)) ||
                        (((x > 265) && (x < 312) && (y > 111) && (y < 165))))
                    {
                        t.Content = "Africa";
                    }
                    else
                        if ((x > 77) && (x < 152) && (y > 54) && (y < 85))
                        {
                            t.Content = "North America";
                        }
                        else
                            if ((x > 419) && (x < 471) && (y > 154) && (y < 178))
                            {
                                t.Content = "Australia";
                            }
                            else
                                if (((x > 341) && (x < 429) && (y > 59) && (y < 94)) ||
                            (((x > 357) && (x < 377) && (y > 93) && (y < 113))))
                                {
                                    t.Content = "Asia";
                                }
                                else
                                    if ((x > 146) && (x < 196) && (y > 124) && (y < 194))
                                    {
                                        t.Content = "South America";
                                    }
                                    else
                                        if ((y > 215) || (y < 30))
                                        {
                                            t.Content = "Antartica";
                                        }
                                        else
                                            if ((x > 252) && (x < 330) && (y > 35) && (y < 69))
                                            {
                                                t.Content = "Europe";
                                            }





                    Console.WriteLine(x.ToString() + "," + y.ToString());

                }
            }

            return HitTestResultBehavior.Continue;
        }
    }
}
